home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_qt.idb / usr / freeware / include / Qt / qglist.h.z / qglist.h
Encoding:
C/C++ Source or Header  |  1998-10-28  |  6.6 KB  |  237 lines

  1. /****************************************************************************
  2. ** $Id: qglist.h,v 2.4 1998/07/03 00:09:45 hanord Exp $
  3. **
  4. ** Definition of QGList and QGListIterator classes
  5. **
  6. ** Created : 920624
  7. **
  8. ** Copyright (C) 1992-1998 Troll Tech AS.  All rights reserved.
  9. **
  10. ** This file is part of Qt Free Edition, version 1.40.
  11. **
  12. ** See the file LICENSE included in the distribution for the usage
  13. ** and distribution terms, or http://www.troll.no/free-license.html.
  14. **
  15. ** IMPORTANT NOTE: You may NOT copy this file or any part of it into
  16. ** your own programs or libraries.
  17. **
  18. ** Please see http://www.troll.no/pricing.html for information about 
  19. ** Qt Professional Edition, which is this same library but with a
  20. ** license which allows creation of commercial/proprietary software.
  21. **
  22. *****************************************************************************/
  23.  
  24. #ifndef QGLIST_H
  25. #define QGLIST_H
  26.  
  27. #ifndef QT_H
  28. #include "qcollection.h"
  29. #endif // QT_H
  30.  
  31.  
  32. /*****************************************************************************
  33.   QLNode class (internal doubly linked list node)
  34.  *****************************************************************************/
  35.  
  36. class QLNode
  37. {
  38. friend class QGList;
  39. friend class QGListIterator;
  40. public:
  41.     GCI        getData()    { return data; }
  42. private:
  43.     GCI        data;
  44.     QLNode *prev;
  45.     QLNode *next;
  46.     QLNode( GCI d )    { data = d; }
  47. };
  48.  
  49.  
  50. /*****************************************************************************
  51.   QGList class
  52.  *****************************************************************************/
  53.  
  54. class QGList : public QCollection        // doubly linked generic list
  55. {
  56. friend class QGListIterator;
  57. friend class QGVector;                // needed by QGVector::toList
  58. public:
  59.     uint  count() const;            // return number of nodes
  60.  
  61.     QDataStream &read( QDataStream & );        // read list from stream
  62.     QDataStream &write( QDataStream & ) const;    // write list to stream
  63.  
  64. protected:
  65.     QGList();                    // create empty list
  66.     QGList( const QGList & );            // make copy of other list
  67.    ~QGList();
  68.  
  69.     QGList &operator=( const QGList & );    // assign from other list
  70.  
  71.     void  inSort( GCI );            // add item sorted in list
  72.     void  append( GCI );            // add item at end of list
  73.     bool  insertAt( uint index, GCI );        // add item at i'th position
  74.     void  relinkNode( QLNode * );        // relink as first item
  75.     bool  removeNode( QLNode * );        // remove node
  76.     bool  remove( GCI = 0 );            // remove item (0=current)
  77.     bool  removeRef( GCI = 0 );            // remove item (0=current)
  78.     bool  removeFirst();            // remove first item
  79.     bool  removeLast();                // remove last item
  80.     bool  removeAt( uint index );        // remove item at i'th position
  81.     GCI      takeNode( QLNode * );            // take out node
  82.     GCI      take();                // take out current item
  83.     GCI      takeAt( uint index );            // take out item at i'th pos
  84.     GCI      takeFirst();                // take out first item
  85.     GCI      takeLast();                // take out last item
  86.  
  87.     void  clear();                // remove all items
  88.  
  89.     int      findRef( GCI, bool = TRUE );        // find exact item in list
  90.     int      find( GCI, bool = TRUE );        // find equal item in list
  91.  
  92.     uint  containsRef( GCI )    const;        // get number of exact matches
  93.     uint  contains( GCI )    const;        // get number of equal matches
  94.  
  95.     GCI      at( uint index );            // access item at i'th pos
  96.     int      at() const;                // get current index
  97.     QLNode *currentNode() const;        // get current node
  98.  
  99.     GCI      get() const;                // get current item
  100.  
  101.     GCI      cfirst() const;            // get ptr to first list item
  102.     GCI      clast()  const;            // get ptr to last list item
  103.     GCI      first();                // set first item in list curr
  104.     GCI      last();                // set last item in list curr
  105.     GCI      next();                // set next item in list curr
  106.     GCI      prev();                // set prev item in list curr
  107.  
  108.     void  toVector( QGVector * ) const;        // put items in vector
  109.  
  110.     virtual int compareItems( GCI, GCI );
  111.  
  112.     virtual QDataStream &read( QDataStream &, GCI & );
  113.     virtual QDataStream &write( QDataStream &, GCI ) const;
  114.  
  115. private:
  116.     void  prepend( GCI );            // add item at start of list
  117.  
  118.     QLNode *firstNode;                // first node
  119.     QLNode *lastNode;                // last node
  120.     QLNode *curNode;                // current node
  121.     int        curIndex;                // current index
  122.     uint    numNodes;                // number of nodes
  123.     QGList *iterators;                // list of iterators
  124.  
  125.     QLNode *locate( uint );            // get node at i'th pos
  126.     QLNode *unlink();                // unlink node
  127. };
  128.  
  129.  
  130. inline uint QGList::count() const
  131. {
  132.     return numNodes;
  133. }
  134.  
  135. inline bool QGList::removeFirst()
  136. {
  137.     first();
  138.     return remove();
  139. }
  140.  
  141. inline bool QGList::removeLast()
  142. {
  143.     last();
  144.     return remove();
  145. }
  146.  
  147. inline int QGList::at() const
  148. {
  149.     return curIndex;
  150. }
  151.  
  152. inline GCI QGList::at( uint index )
  153. {
  154.     QLNode *n = locate( index );
  155.     return n ? n->data : 0;
  156. }
  157.  
  158. inline QLNode *QGList::currentNode() const
  159. {
  160.     return curNode;
  161. }
  162.  
  163. inline GCI QGList::get() const
  164. {
  165.     return curNode ? curNode->data : 0;
  166. }
  167.  
  168. inline GCI QGList::cfirst() const
  169. {
  170.     return firstNode ? firstNode->data : 0;
  171. }
  172.  
  173. inline GCI QGList::clast() const
  174. {
  175.     return lastNode ? lastNode->data : 0;
  176. }
  177.  
  178.  
  179. /*****************************************************************************
  180.   QGList stream functions
  181.  *****************************************************************************/
  182.  
  183. QDataStream &operator>>( QDataStream &, QGList & );
  184. QDataStream &operator<<( QDataStream &, const QGList & );
  185.  
  186.  
  187. /*****************************************************************************
  188.   QGListIterator class
  189.  *****************************************************************************/
  190.  
  191. class QGListIterator                // QGList iterator
  192. {
  193. friend class QGList;
  194. protected:
  195.     QGListIterator( const QGList & );
  196.     QGListIterator( const QGListIterator & );
  197.     QGListIterator &operator=( const QGListIterator & );
  198.    ~QGListIterator();
  199.  
  200.     bool  atFirst() const;            // test if at first item
  201.     bool  atLast()  const;            // test if at last item
  202.     GCI      toFirst();                // move to first item
  203.     GCI      toLast();                // move to last item
  204.  
  205.     GCI      get() const;                // get current item
  206.     GCI      operator()();                // get current and move to next
  207.     GCI      operator++();                // move to next item (prefix)
  208.     GCI      operator+=(uint);            // move n positions forward
  209.     GCI      operator--();                // move to prev item (prefix)
  210.     GCI      operator-=(uint);            // move n positions backward
  211.  
  212. protected:
  213.     QGList *list;                // reference to list
  214.  
  215. private:
  216.     QLNode  *curNode;                // current node in list
  217. };
  218.  
  219.  
  220. inline bool QGListIterator::atFirst() const
  221. {
  222.     return curNode == list->firstNode;
  223. }
  224.  
  225. inline bool QGListIterator::atLast() const
  226. {
  227.     return curNode == list->lastNode;
  228. }
  229.  
  230. inline GCI QGListIterator::get() const
  231. {
  232.     return curNode ? curNode->data : 0;
  233. }
  234.  
  235.  
  236. #endif    // QGLIST_H
  237.